package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dto.AdminImpl;
import cn.edu.cqupt.cxcy.model.Notice;
import cn.edu.cqupt.cxcy.util.Validate;


/**
 * 管理员后台主页
 * @author Dancer-mno
 * @date 2015年10月17日
 */
public class AdminAddNews extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//  /admin/addnews
		//  /admin/addnews?ntid=?&&title=?&&content=?  ntid为公告类型,title为标题,content为内容
		int nid = Validate.stringIsNO(request.getParameter("nid"))?Integer.parseInt(request.getParameter("nid")):0;//电话
		String n_title  = request.getParameter("title") != null ? request.getParameter("title") : "";
		String n_from  = request.getParameter("from") != null ? request.getParameter("from") : "";
		String n_content  = request.getParameter("content") != null ? request.getParameter("content") : "";
		int nt_id = Validate.stringIsNO(request.getParameter("ntid"))?Integer.parseInt(request.getParameter("ntid")):1;//电话
		int a_id = (int) ((List<Object>) request.getSession().getAttribute( "user")).get(1);
		Notice ni = new Notice();
		ni.setA_id(a_id);
		ni.setN_from(n_from);
		ni.setNt_id(nt_id);
		ni.setN_title(n_title);
		ni.setN_content(n_content);
		if(nid != 0) {
			ni.setN_id(nid);
			new AdminImpl().updateNew(ni);;
		} else {
			new AdminImpl().addNews(ni);
		}
		response.sendRedirect(request.getHeader("referer"));
	}
}
